#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/centos7-install-php81-pdns.sh | /bin/bash
#
#
############################################################################

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

mkdir -p "${HOME}"/bao
wget -P "${HOME}"/bao/  http://file.ttpai.work/d/tar/pdns/pdns-4.1.x-linux.tar.gz
tar -zxvf "${HOME}"/bao/pdns-4.1.x-linux.tar.gz -C "${HOME}"/bao/
yum install -y "${HOME}"/bao/pdns-4.1/*



yum install -y mariadb



local_ip=`ip address show | awk '/inet / {print $2}' | grep -v 127.0.0.1 | cut -d'/' -f1`
cp /etc/pdns/pdns.conf /etc/pdns/pdns.conf.bak
cat > /etc/pdns/pdns.conf <<EOF
#这里是最低限度的配置，剩下的不填写就是默认选项，默认配置可以参见：/etc/pdns/pdns.conf.bak

daemon=yes
guardian=yes
launch=gmysql
gmysql-host=192.168.0.1     # 设置MySQL服务器地址
gmysql-port=3306            # 设置MySQL服务器端口
gmysql-user=powerdns41      # 设置MySQL用户名
gmysql-dbname=powerdns41    # 设置MySQL数据库名
gmysql-password=123456    # 设置MySQL密码
local-address=${local_ip}
log-dns-queries=yes
non-local-bind=yes
query-logging=yes
setgid=pdns
setuid=pdns
webserver=yes
webserver-address=0.0.0.0
webserver-password=hello@123456
api-key=hello@123456
api=yes
api-logfile=/var/log/pdns.log
EOF


cat <<\Tay

====================================
可以使用api来确定【pdns-权威服务器】是否安装OK
====================================

注意：数据库连接地址没有配置（配置文件中是随意写的）
路径：/etc/pdns/pdns.conf

systemctl restart pdns.service
systemctl status pdns.service
systemctl enable pdns.service

curl -v -H 'X-API-Key: hello@123456' http://127.0.0.1:8081/api/v1/servers


====================================
如果没有创建数据库，需要先创建一下库和表
====================================

### 建库 + 建用户
CREATE DATABASE powerdns41 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER `powerdns41`@`%` IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON `powerdns41`.* TO `powerdns41`@`%`;



### 验证行号密码
yum install -y mariadb
mysql -h 172.16.2.39 -u powerdns41 -p
Password:
> show databases;
> use powerdns41;
> show tables;
> exit;



### 建表
mysql -h 172.16.2.39 -u powerdns41 -p  powerdns41  <  /usr/share/doc/pdns-backend-mysql-4.1.14/schema.mysql.sql



Tay
